<?php

<script>
console.debug("postUser");
</script>

$roundId;

$db = mysql_connect('localhost', 'kattilam_test', 'test') or die ('Unable to connect to DB!!');
mysql_select_db('kattilam_discgolfmobile', $db) or die (mysql_error($db));

$query = 'INSERT INTO round (date, course) VALUES (Now( ), "' . $_REQUEST['course'] .'")';
mysql_query($query, $db) or die (mysql_error($db));

$query2 = 'SELECT MAX(id) FROM round';
$result2 = mysql_query($query2, $db) or die (mysql_error($db));

while ($row = mysql_fetch_array($result2)) {
  extract($row);
  $roundId = $row[0];
  }

$query3 = 'INSERT INTO result (round_id, player_name) VALUES('. $roundId . ',"' . $_REQUEST['player1'] . '")';
mysql_query($query3, $db) or die (mysql_error($db));  

if ($_REQUEST['player2'] != null) {
  $query4 = 'INSERT INTO result (round_id, player_name) VALUES('. $roundId . ',"' . $_REQUEST['player2'] . '")';
  mysql_query($query4, $db) or die (mysql_error($db));  
}

if ($_REQUEST['player3'] != null) {
  $query5 = 'INSERT INTO result (round_id, player_name) VALUES('. $roundId . ',"' . $_REQUEST['player3'] . '")';
  mysql_query($query5, $db) or die (mysql_error($db));  
}

if ($_REQUEST['player4'] != null) {
  $query6 = 'INSERT INTO result (round_id, player_name) VALUES('. $roundId . ',"' . $_REQUEST['player4'] . '")';
  mysql_query($query6, $db) or die (mysql_error($db));  
}

/*
header('Content-Type: application/json');
if($pw != ''){
	echo '{"success":true, "msg":'.json_encode('This User is authorized').'}';
}else{
	echo '{"success":false, "msg":'.
		json_encode('This User is NOT authorized').
		', "errors" : { "password" :'.json_encode('Password is required').
		'}'.
		', "pwd" :'.json_encode($pw).'}';
}
 */
?>